package whf.shop.utils;

import whf.shop.entity.Currency;
import whf.shop.service.CurrencyServiceImp;

/**
 * @author wanghaifeng
 * @create Jan 12, 2007 10:03:55 AM
 * 
 */
public final class CurrencyUtils {
	private CurrencyUtils(){
	}
	
	/**
	 * 将一种货币转换成基本货币后的数字
	 * @modify wanghaifeng Jan 12, 2007 9:57:50 AM
	 * @param input 输入的原货币数量
	 * @param cur 原货币
	 * @return 基本货币的数量
	 */
	public static double calculate(double input, Currency cur){
		try{
			Currency baseCur = CurrencyServiceImp.getCurrencyService().findBaseCurrency();
			if(cur== null || cur.getId() == baseCur.getId()) return input;
			return input * baseCur.getExchangeRate() / cur.getExchangeRate() ;
		}catch(Exception e){
			return input;
		}
	}
	
	/**
	 * 根据汇率计算
	 * @modify wanghaifeng Jan 12, 2007 10:02:45 AM
	 * @param input
	 * @param exchangeRate
	 * @return 转换汇率后的数量 
	 */
	public static double calculate(double input, double exchangeRate){
		try{
			Currency baseCur = CurrencyServiceImp.getCurrencyService().findBaseCurrency();
			return input * exchangeRate / baseCur.getExchangeRate();
		}catch(Exception e){
			return input;
		}
	}
}
